﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ld.Server.Dto;
using Ld.Server.Service;
using Ld.Server.Contract;
using System.Threading.Tasks;

namespace Ld.Server.Facade.Implement
{
    public partial class LdServerFacade : ILdServerContract
    {
        public IProductManager ProductManager { get; set; }

        public Product[] LoadAllProduct()
        {
            var list = ProductManager.LoadAll().Select(entity => new Product
            {
                ID = entity.ID,
                Code = entity.Code,
                Name = entity.Name,
                SellPrice = entity.SellPrice,
                BuyPrice = entity.BuyPrice,
                QuantityPerUnit = entity.QuantityPerUnit,
                Unit = entity.Unit,
                Remark = entity.Remark
            });

            return list.ToArray();
        }

        public void SaveProduct(Product entity)
        {
            var model = new Ld.Server.Domain.Entities.Product
            {
                ID = entity.ID,
                Code = entity.Code,
                Name = entity.Name,
                SellPrice = entity.SellPrice,
                BuyPrice = entity.BuyPrice,
                QuantityPerUnit = entity.QuantityPerUnit,
                Remark = entity.Remark,
                Unit = entity.Unit
            };

            this.ProductManager.Save(model);

            this.ReceiveProduct(entity);
        }

        private void ReceiveProduct(Product entity)
        {
            try
            {
                Parallel.ForEach(callBackList, (item) =>
                {
                    try
                    {
                        item.ReceiveProduct(entity);
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                    }
                });
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
        }

        public void UpdateProduct(Product entity)
        {
            var model = this.ProductManager.Get(entity.ID);
            if (model == null)
            {
                return;
            }

            model.Code = entity.Code;
            model.Name = entity.Name;
            model.SellPrice = entity.SellPrice;
            model.BuyPrice = entity.BuyPrice;
            model.QuantityPerUnit = entity.QuantityPerUnit;
            model.Remark = entity.Remark;
            model.Unit = entity.Unit;

            this.ProductManager.Update(model);

            this.ReceiveProduct(entity);
        }

        public void DeleteProduct(Guid id)
        {
            var entity = this.ProductManager.Get(id);
            if (entity == null)
            {
                return;
            }

            this.ProductManager.Delete(entity);

            try
            {
                Parallel.ForEach(callBackList, (item) =>
                {
                    try
                    {
                        item.ClearProduct(id);
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                    }
                });
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
        }
    }
}
